/**
 * 
 */
package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.PsbcRespFieldConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.psbc.ebank.PsbcB2bMessage;

/**
 * @PsbcB2bFrontRes.java
 * @author Ryan
 * @2013-12-3 下午4:41:15 www.gopay.com.cn Inc.All rights reserved.
 */
@Scope("prototype")
@Controller
public class PsbcB2bFrontRes extends BankResBase {

	private static Log logger = LogFactory.getLog(PsbcB2bFrontRes.class);

	@Override
	@RequestMapping("/psbcB2bFrontRes")
	protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
		logger.error("--------------- PsbcB2bFrontRes bankback ---------------");	
		return super.execute(request, response);
	}

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		PsbcB2bMessage message = new PsbcB2bMessage();
		message.setFront(true);
		message.setOrderId(request.getParameter("OrderNo"));
		message.setRespCode(request.getParameter("ResponseCode"));
		message.setBankOrderId(message.getOrderId());
		// 银行返回的信息
		message.setPlainMessage(request.getParameter("Plain")); // 支付响应明文
		message.setSignMessage(request.getParameter("Signature")); // 支付响应密文
		Map<String, String> map = parseMesage(message); // 解决明文
		map2Bean(message, map);
		message.setAmount(new BigDecimal(message.getTranAmt()));
		//银行业务产品01企业网银支付
        message.setBankBusProd(BisBankBusinessProd._01);
		return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		OrderInfo orderInfo = new OrderInfo();
		PsbcB2bMessage message = (PsbcB2bMessage) result.getModel("message");
		orderInfo.setOrderId(message.getOrderId());
		orderInfo.setAmt(String.valueOf(message.getAmount().longValue()));
		orderInfo.setNotifyNum("1");
		orderInfo.setPayStatus("");
		orderInfo.setRetCode("00");
		orderInfo.setRetMsg("");
		return orderInfo;
	}

	public Map<String, String> parseMesage(PsbcB2bMessage message) {
		String plain = message.getPlainMessage();
		if (null == plain || "".equals(plain)) {
			return null;
		}
		LinkedHashMap<String, String> retMap = new LinkedHashMap<String, String>();
		String[] items = plain.split("\\|");
		for (String item : items) {
			int index = item.indexOf("=");
			retMap.put(item.substring(0, index), item.substring(index + 1));
		}

		return retMap;
	}

	/**
	 * process map entry to bean property conversion ,only supports 6 type as
	 * follows: String ,Integer/int
	 * ,Double/double,Long/long,Float/float,Boolean/boolean,
	 * 
	 * @param t
	 * @param param
	 */
	@SuppressWarnings("null")
	public void map2Bean(PsbcB2bMessage t, Map<String, String> param) {
		if (param != null && !param.isEmpty()) {
			for (Entry<String, String> entry : param.entrySet()) {
				if (PsbcRespFieldConstants.RSP_TRANABBR.equals(entry.getKey())) {
					t.setTranAbbr(entry.getValue());
				} else if (PsbcRespFieldConstants.RSP_MERCDTTM.equals(entry
						.getKey())) {
					t.setMercCode(entry.getValue());
				} else if (PsbcRespFieldConstants.RSP_TERMSSN.equals(entry
						.getKey())) {
					t.setTermSsn(entry.getValue());
				} else if (PsbcRespFieldConstants.RSP_RESPCODE.equals(entry
						.getKey())) {
					t.setRespCode(entry.getValue());
				} else if (PsbcRespFieldConstants.RSP_TERMCODE.equals(entry
						.getKey())) {
					t.setTermCode(entry.getValue());
				} else if (PsbcRespFieldConstants.RSP_MERCCODE.equals(entry
						.getKey())) {
					t.setMercCode(entry.getValue());
				} else if (PsbcRespFieldConstants.RSP_TRANAMT.equals(entry
						.getKey())) {
					t.setTranAmt(entry.getValue());
				} else if (PsbcRespFieldConstants.RSP_SETTDATE.equals(entry
						.getKey())) {
					t.setSettDate(entry.getValue());
				}
			}
		}
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if (StringUtils.isEmpty(request.getParameter("Plain"))|| StringUtils.isEmpty(request.getParameter("Signature"))) {
			return false;
		}
		return true;
	}
}
